package com.lysj.order.order.service;

import com.lysj.admin.utils.exception.BaseException;
import com.lysj.admin.utils.spring.foundation.base.BaseService;
import com.lysj.admin.utils.spring.foundation.pagination.PageUtil;
import com.lysj.admin.utils.spring.foundation.pagination.PageVo;
import com.lysj.admin.utils.web.Resp;
import com.lysj.order.order.domain.OrderItem;
import com.lysj.order.order.repository.OrderItemRepository;
import com.lysj.order.order.vo.WorkBenchOrderItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author Created by drj on 2019-03-13 10:43:06
 * @description 订单详情服务层
 */
@Slf4j
@Service
@Transactional
public class OrderItemService implements BaseService<OrderItem> {

    @Resource
    private OrderItemRepository orderItemRepository;

    @Override
    public OrderItemRepository getRepository() {
        return orderItemRepository;
    }


    /*
     * @author drj
     * @date 2019-03-13 11:54
     * @Description :加菜
     */
    public void addOrderItem(List<OrderItem> orderItemList) {
        //初始化菜品状态
        for (OrderItem orderItem : orderItemList) {
            orderItem.setStatus(OrderItem.Status.GOODS.getCode());
        }
        orderItemRepository.save(orderItemList);
    }

    /*
     * @author drj
     * @date 2019-03-13 14:31
     * @Description:查看点餐进度
     */
    public Page<WorkBenchOrderItemVo> scheduleList(String orderId, Integer status, PageVo pageVo) {
        Pageable pageable = PageUtil.initPage(pageVo);
        Page<WorkBenchOrderItemVo> orderItemPage = orderItemRepository.findByOrderIdAndStatus(pageable, orderId, status);
        return orderItemPage;
    }

    //赠菜或退菜
    public String giftOrCancle(String orderItemId, Integer status) {
        OrderItem orderItem = orderItemRepository.findOne(orderItemId);
        if (null == orderItem) {
            throw new BaseException("赠菜或退菜异常,请联系商户处理");
        }
        orderItem.setStatus(status);
        orderItemRepository.save(orderItem);
        return "操作成功";
    }


}